﻿
Imports Windows.Storage
Imports Windows.Storage.Pickers
Imports Windows.Storage.Search

' The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

''' <summary>
''' An empty page that can be used on its own or navigated to within a Frame.
''' </summary>
Public NotInheritable Class MainPage
    Inherits Page

    Private Shared ReadOnly supported_file_types() As String = Nothing

    Shared Sub New()
        supported_file_types = New String() {
            ".mp3",
            ".wma",
            ".mp4",
            ".wmv",
            ".avi"
        }
    End Sub

    Private Sub add_files(ByVal files As IReadOnlyList(Of StorageFile))
        If Not files Is Nothing AndAlso files.Count() > 0 Then
            For Each f In files
                If supported_file_types.Contains(f.FileType()) Then
                    push_back(f)
                End If
            Next
        End If
    End Sub

    Private Shared Sub filetype_filter(ByVal ff As IList(Of String))
        For Each i In supported_file_types
            ff.Add(i)
        Next
    End Sub

    Private Async Sub add_file_Click(sender As Object, e As RoutedEventArgs) Handles add_file.Click
        Dim fp As FileOpenPicker = Nothing
        fp = New FileOpenPicker()
        fp.SuggestedStartLocation() = PickerLocationId.MusicLibrary
        filetype_filter(fp.FileTypeFilter())
        fp.ViewMode() = PickerViewMode.List
        add_files(Await fp.PickMultipleFilesAsync())
    End Sub

    Private Async Sub add_folder_Click(sender As Object, e As RoutedEventArgs) Handles add_folder.Click
        Dim fp As FolderPicker = Nothing
        fp = New FolderPicker()
        fp.SuggestedStartLocation() = PickerLocationId.MusicLibrary
        filetype_filter(fp.FileTypeFilter())
        fp.ViewMode() = PickerViewMode.List
        Dim sf As StorageFolder = Nothing
        sf = Await fp.PickSingleFolderAsync()
        If Not sf Is Nothing Then
            add_files(Await sf.GetFilesAsync(CommonFileQuery.OrderByName))
        End If
    End Sub
End Class
